<?php
/**
 * (commands) 财务报表V2。报告内容周期14天更新 (reports类型：只获取Amazon提供的数据，无需创建报告。)
 */
namespace App\Console\Commands\SpApi\Report\CreateAndGet;

use App\Crontab\Daily\SpApi\GrabGetReports;
use App\Models\Amazon\AmazonReportCondition;
use Illuminate\Console\Command;
use SellingPartnerApi\ReportType;

class SettlementDeposit extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sp-report:settlement-deposit {startDate=null} {endDate=null} {accountId=0}';

    /**
     * The console command description.
     * @var string
     */
    protected $description = 'exe func';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $reportType = ReportType::GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2;

        $where = [];
        $startDate = $this->argument('startDate');
        $endDate   = $this->argument('endDate');
        $accountId = $this->argument('accountId');
        if($accountId && $accountId > 0){
            $where['id'] = $accountId;
        }

        $roportLastUpdateTime = '2024-12-01';
        $startDates = $startDate != 'null' ? $startDate : $roportLastUpdateTime;
        $endDates = $endDate != 'null' ? $endDate : date('Y-m-d', strtotime("-2 days"));
        if (!strtotime($startDates) || !strtotime($endDates)) return false;

        $startTime1 = getDateFormat(date('Y-m-d 00:00:00', strtotime($startDates)));
        $endTime1 = getDateFormat(date('Y-m-d 23:59:59', strtotime($endDates)));

        GrabGetReports::grab([$reportType['name']],$startTime1,$endTime1,$where);

        ////获取报告时间
        //$roportLastUpdateTime = AmazonReportCondition::getStartTime($reportType['name']);
        //if (!strtotime($roportLastUpdateTime)) return false;

        /*$startTimes = getDateFormat(date('Y-m-d 00:00:00', strtotime($startDates)));
        $endTimes = getDateFormat(date('Y-m-d 23:59:59', strtotime($endDates)));
        $bof = strtotime($startDates); //开始
        $eof = strtotime($endDates); //结束
        while(true){
            if ($bof > $eof) break;
            $time = $bof;
            $startTime = getDateFormat(date('Y-m-d 00:00:00', $time));
            $endTime = getDateFormat(date('Y-m-d 23:59:59', $time));

            echo '------start:'.$startTime.PHP_EOL;
            GrabGetReports::grab([$reportType['name']],$startTime,$endTime,$where);

            $bof += 86400;
            sleep(2);
        }*/

        ////更新报告时间
        //AmazonReportCondition::upStartTime($reportType['name'],$endDates);

        $this->info('finish!');

        //GrabGetReports::grab([$reportType['name']],$startTime,$endTime,$where);
        //$this->info('finish!');
    }
}
